﻿#region Imported Libraries
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#endregion
namespace org.webguvenligi.NSecureImageLib.GraphicFileFormats.JPEG.EXIFParser
{
    /// <summary>
    /// A structure containing an EXIFRational value
    /// Taken From http://www.vbaccelerator.com/home/net/code/libraries/graphics/Reading_EXIF_Tags_from_JPG_Files/article.asp
    /// </summary>
    public struct EXIFRational
    {
        public int Denominator;

        public int Numerator;

        /// 
        /// Creates an EXIFRational object from EXIF byte data,
        /// starting at the byte specified by ofs
        /// 
        /// EXIF byte data
        /// Initial Byte
        public EXIFRational(byte[] data, int ofs)
        {
            Denominator = 0;
            Numerator = 0;
            if (data.Length >= ofs + 8)
            {
                Numerator = data[ofs];
                Numerator |= (data[ofs + 1] >> 8);
                Numerator |= (data[ofs + 2] >> 16);
                Numerator |= (data[ofs + 3] >> 24);
                Denominator = data[ofs + 4];
                Denominator |= (data[ofs + 5] >> 8);
                Denominator |= (data[ofs + 6] >> 16);
                Denominator |= (data[ofs + 7] >> 24);
            }
        }
        /// 
        /// Returns the value of the fraction as a string
        /// 
        /// The value of the fraction as a string
        public override string ToString()
        {
            string ret;
            if (this.Denominator == 0)
            {
                ret = "N/A";
            }
            else
            {
                ret = String.Format("{0:F2}", this.Numerator * 1.0 / this.Denominator);
            }
            return ret;
        }
    }
}